package com.onecar.carplatform.common.protocol.jt808;

import com.onecar.carplatform.common.constants.Jt808MsgId;
import com.onecar.carplatform.common.protocol.annotations.JtMsgDesc;
import com.onecar.carplatform.common.protocol.basics.JtMessage;
import com.onecar.carplatform.common.utils.JtByteBufUtil;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.ToString;

/**
 * @Author : ljq
 * @Description : 终端注册消息体
 * @Date : 2021/8/20 11:02
 * @Version : 1.0
 */
@Data
@ToString(callSuper = true)
@JtMsgDesc(msgId = Jt808MsgId.TERMINAL_REGISTER)
public class Jt808Msg0100 extends JtMessage {
    private int provinceId; // 省域 ID
    private int cityId; // 市县域 ID
    private String manufacturerId; // 制造商 ID
    private String terminalModel; // 终端型号
    private String terminalId; // 终端 ID
    private byte licencePlateColor; // 车牌颜色
    private String licencePlate; // 车牌

    @Override
    public byte[] encodeMsgBody() {
        return new byte[0];
    }

    @Override
    public void decodeMsgBody(ByteBuf buf) {
        provinceId = buf.readShort();
        cityId = buf.readShort();
        // Version before 2019
        if (header.getVersion() == 0) {
            manufacturerId = JtByteBufUtil.buf2StringGbk(buf, 5);
            // 2011 和 2013 版本没有办法区分，用长度来判断，打印 37 的定位 2013 版本
            if (header.getMsgBodyLen() > 37) {
                terminalModel = JtByteBufUtil.buf2StringGbk(buf, 20);
            } else {
                terminalModel = JtByteBufUtil.buf2StringGbk(buf, 8);
            }
            terminalId = JtByteBufUtil.buf2StringGbk(buf, 7);
        } else {
            manufacturerId = JtByteBufUtil.buf2StringGbk(buf, 11);
            terminalModel = JtByteBufUtil.buf2StringGbk(buf, 30);
            terminalId = JtByteBufUtil.buf2StringGbk(buf, 30);
        }
        licencePlateColor = buf.readByte();
        licencePlate = JtByteBufUtil.buf2StringGbk(buf, buf.readableBytes());
    }
}
